package org.robot.scm.platform.server.handler;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.util.Map;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.enums.PlatformEnum;
import org.robot.scm.utils.CallHelper;

@Slf4j
@NoArgsConstructor
public class CallVoIPHandler extends BaseHandler implements HttpHandler {


    @Override
    public String getUri() {
        return "/call";
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {
        if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) {
            sendFailureResponse(exchange, "请求非法");
            return;
        }

        Map<String, String> queryMap = queryToMap(exchange.getRequestURI().getQuery());
        String wxid = queryMap.get("wxid");
        if (StringUtils.isBlank(wxid)) {
            sendFailureResponse(exchange, "对方微信不可为空");
            return;
        }


        if (CallHelper.callById(config(PlatformEnum.ROBOT), wxid)) {
            sendSuccessResponse(exchange, "拨打语音电话成功");
        }

        sendFailureResponse(exchange, "拨打语音电话失败");
    }
}
